package com.xtgg.more;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.xtgg.R;
import com.xtgg.R.layout;
import com.xtgg.R.menu;
import com.xtgg.login.LoginActivity;
import com.xtgg.manager.BaseActivity;
import com.xtgg.model.Resp;
import com.xtgg.sys.Constants;
import com.xtgg.sys.DemoApp;
import com.xtgg.util.MgqDataHandler;
import com.xtgg.util.MgqRestClient;
import com.xtgg.util.ToastUtil;
import com.xtgg.util.Utils;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MessageDetailActivity extends BaseActivity implements
		OnClickListener {
	JSONObject mInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_detail);

		((TextView) findViewById(R.id.center)).setText("消息详情");
		((Button) findViewById(R.id.right)).setText("删除");

		findViewById(R.id.left).setOnClickListener(this);
		findViewById(R.id.right).setOnClickListener(this);
		findViewById(R.id.unread).setOnClickListener(this);

		mInfo = JSON.parseObject(getIntent().getStringExtra("info"));

		((TextView) findViewById(R.id.time)).setText(Utils.formatDate(
				mInfo.getString("pushtime"), "yyyy/MM/dd HH:mm"));
		((TextView) findViewById(R.id.content)).setText(mInfo
				.getString("content"));
		delHttpRequest("1");
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.left:
			finish();
			break;

		case R.id.right:
			showQuery();
			break;

		case R.id.unread:
			delHttpRequest("1");
			break;
		}
	}

	void showQuery() {
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_query);
		mDialog.findViewById(R.id.left).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
					}
				});
		mDialog.findViewById(R.id.right).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						mDialog.dismiss();
						delHttpRequest("9");
					}
				});
		((TextView) mDialog.findViewById(R.id.content)).setText("删除该消息？");
		mDialog.show();
	}

	void delHttpRequest(final String info) {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				Resp ruser = JSON.parseObject(response, Resp.class);

				if (ruser.getState().equals("1")) {
					if (info.equals("9")) {
						List<Activity> list = ((DemoApp) getApplicationContext())
								.getActivityList();
						((MessageCenterActivity) list.get(list.size() - 2))
								.reloadData();
						finish();
					}
				}
				else if (ruser.getState().equals("2")) 
				{showExitDialog();} else {
					ToastUtil.showToastWithAlertPic(ruser.getMessage());
				}
			}
		};

		RequestParams params = new RequestParams();
		params.put("act", "operateAppLog");
		params.put("type", info);
		params.put("pushid", mInfo.getString("pushid"));

		MgqRestClient.post(Constants.INFOMGT, params, loginHandler);
	}
}
